/* This is a small demo of the high-performance GUIX graphics framework. */

#include <stdio.h>
#include "tx_api.h"
#include "gx_api.h"
#include "gx_validation_utility.h"
#include "gx_system.h"

TEST_PARAM test_parameter = {
    "guix_canvas_pixelmap_blend", /* Test name */
    41, 31, 597, 471  /* Define the coordinates of the capture area.
                         In this test, we only need to capture the pixelmap
                         drawing area.  */
};

static VOID      control_thread_entry(ULONG);
int main(int argc, char ** argv)
{
    /* Parse the command line argument. */
    gx_validation_setup(argc, argv);

    /* Start ThreadX system */
    tx_kernel_enter(); 
    return(0);
}

VOID tx_application_define(void *first_unused_memory)
{
    /* Call the actual line example routine. */
    gx_validation_application_define(first_unused_memory);

    /* Termiante the test if it runs for more than 100 ticks */
    /* This function is not implemented yet. */
    gx_validation_watchdog_create(100);

    /* Create a dedicated thread to perform various operations
       on the line drawing example. These operations simulate 
       user input. */
    gx_validation_control_thread_create(control_thread_entry);

}


/* Replace the default graphics driver with the validation driver. */
#ifdef win32_graphics_driver_setup_24xrgb
#undef win32_graphics_driver_setup_24xrgb  
#endif
#define win32_graphics_driver_setup_24xrgb  gx_validation_graphics_driver_setup_24xrgb


#ifdef WIN32
#undef WIN32
#endif

#include "gx_validation_wrapper.h"
#include "demo_guix_all_widgets.c"


static GX_CONST GX_UBYTE PRIMARY_THEME_1_ALPHAMAP_pixelmap_data[469] =
{
    0xa5, 0x00, 0xa5, 0x00, 0x93, 0x00, 0x01, 0x52, 0xe2, 0x82, 0xff, 0x01, 0xfb, 
    0xb5, 0x8a, 0x00, 0x92, 0x00, 0x00, 0xc5, 0x86, 0xff, 0x01, 0xf7, 0x0b, 0x88, 
    0x00, 0x91, 0x00, 0x00, 0x89, 0x88, 0xff, 0x01, 0xf3, 0x14, 0x87, 0x00, 0x90, 
    0x00, 0x01, 0x11, 0xfe, 0x89, 0xff, 0x01, 0xb4, 0x12, 0x86, 0x00, 0x90, 0x00, 
    0x00, 0xe2, 0x8a, 0xff, 0x02, 0xf6, 0x45, 0x08, 0x85, 0x00, 0x8f, 0x00, 0x00, 
    0x6f, 0x8b, 0xff, 0x02, 0xfc, 0x82, 0x1e, 0x85, 0x00, 0x8f, 0x00, 0x00, 0xfe, 
    0x8b, 0xff, 0x02, 0xfd, 0xa6, 0x36, 0x85, 0x00, 0x8e, 0x00, 0x00, 0xe2, 0x8c, 
    0xff, 0x02, 0xfc, 0xb4, 0x40, 0x85, 0x00, 0x8d, 0x00, 0x00, 0x70, 0x8d, 0xff, 
    0x02, 0xf5, 0xb4, 0x40, 0x85, 0x00, 0x8d, 0x00, 0x00, 0xfe, 0x8c, 0xff, 0x03, 
    0xfe, 0xe4, 0xa7, 0x37, 0x85, 0x00, 0x8c, 0x00, 0x00, 0xe3, 0x8d, 0xff, 0x03, 
    0xf2, 0xd8, 0x83, 0x1e, 0x85, 0x00, 0x8b, 0x00, 0x00, 0x90, 0x8d, 0xff, 0x04, 
    0xfb, 0xe7, 0xb4, 0x4b, 0x08, 0x85, 0x00, 0x8a, 0x00, 0x01, 0x18, 0xfe, 0x8c, 
    0xff, 0x04, 0xfe, 0xec, 0xce, 0x74, 0x17, 0x86, 0x00, 0x8a, 0x00, 0x00, 0xf0, 
    0x8d, 0xff, 0x04, 0xf2, 0xdd, 0x98, 0x32, 0x02, 0x86, 0x00, 0x89, 0x00, 0x00, 
    0xad, 0x8d, 0xff, 0x04, 0xfb, 0xe7, 0xb6, 0x50, 0x0b, 0x87, 0x00, 0x89, 0x00, 
    0x00, 0xfe, 0x8c, 0xff, 0x04, 0xfe, 0xec, 0xce, 0x74, 0x17, 0x88, 0x00, 0x88, 
    0x00, 0x00, 0xe6, 0x8d, 0xff, 0x04, 0xf2, 0xdd, 0x99, 0x32, 0x03, 0x88, 0x00, 
    0x87, 0x00, 0x00, 0x81, 0x8d, 0xff, 0x04, 0xfb, 0xe7, 0xb7, 0x51, 0x0b, 0x89, 
    0x00, 0x87, 0x00, 0x00, 0xfe, 0x8d, 0xff, 0x03, 0xec, 0xcf, 0x74, 0x18, 0x8a, 
    0x00, 0x86, 0x00, 0x00, 0xe7, 0x8d, 0xff, 0x04, 0xf5, 0xdf, 0x9b, 0x32, 0x03, 
    0x8a, 0x00, 0x85, 0x00, 0x00, 0x84, 0x8d, 0xff, 0x04, 0xfc, 0xe8, 0xbd, 0x56, 
    0x0b, 0x8b, 0x00, 0x85, 0x00, 0x00, 0xfc, 0x8d, 0xff, 0x03, 0xec, 0xd3, 0x7e, 
    0x1e, 0x8c, 0x00, 0x84, 0x00, 0x00, 0x69, 0x8d, 0xff, 0x04, 0xf7, 0xe0, 0xa1, 
    0x39, 0x05, 0x8c, 0x00, 0x84, 0x00, 0x00, 0xb3, 0x8c, 0xff, 0x04, 0xfd, 0xe9, 
    0xc0, 0x5a, 0x0d, 0x8d, 0x00, 0x84, 0x00, 0x00, 0xb8, 0x8c, 0xff, 0x03, 0xed, 
    0xd7, 0x86, 0x22, 0x8e, 0x00, 0x84, 0x00, 0x00, 0x7f, 0x8b, 0xff, 0x04, 0xf7, 
    0xe3, 0xaa, 0x43, 0x07, 0x8e, 0x00, 0x84, 0x00, 0x00, 0x25, 0x8a, 0xff, 0x04, 
    0xfe, 0xea, 0xc4, 0x61, 0x11, 0x8f, 0x00, 0x84, 0x00, 0x01, 0x16, 0xd7, 0x89, 
    0xff, 0x03, 0xed, 0xd7, 0x89, 0x24, 0x90, 0x00, 0x84, 0x00, 0x02, 0x06, 0x53, 
    0xf7, 0x87, 0xff, 0x04, 0xf3, 0xe3, 0xab, 0x43, 0x07, 0x90, 0x00, 0x85, 0x00, 
    0x02, 0x2a, 0x91, 0xf1, 0x84, 0xff, 0x05, 0xfd, 0xf0, 0xe8, 0xc0, 0x60, 0x11, 
    0x91, 0x00, 0x85, 0x00, 0x0c, 0x0a, 0x46, 0xa1, 0xd7, 0xf0, 0xf5, 0xf6, 0xef, 
    0xed, 0xe3, 0xc1, 0x72, 0x1c, 0x92, 0x00, 0x86, 0x00, 0x0b, 0x0f, 0x44, 0x8b, 
    0xbd, 0xd2, 0xd7, 0xd5, 0xc9, 0xa4, 0x61, 0x1f, 0x04, 0x92, 0x00, 0x87, 0x00, 
    0x09, 0x09, 0x26, 0x4c, 0x6a, 0x78, 0x72, 0x59, 0x35, 0x12, 0x01, 0x93, 0x00, 
    0x89, 0x00, 0x04, 0x04, 0x0d, 0x14, 0x11, 0x06, 0x96, 0x00, 0xa5, 0x00, 0xa5, 
    0x00
};
GX_CONST GX_PIXELMAP PRIMARY_THEME_1_ALPHAMAP_pixelmap =
{
    0x00000001,                              /* major version                  */
    0x00000000,                              /* minor version                  */
    GX_PIXELMAP_COMPRESSED|GX_PIXELMAP_ALPHA,         /* flags                 */
    GX_COLOR_FORMAT_8BIT_ALPHAMAP,           /* Format                         */
    (GX_UBYTE *) PRIMARY_THEME_1_ALPHAMAP_pixelmap_data,
    sizeof(PRIMARY_THEME_1_ALPHAMAP_pixelmap_data),    /* the size of pixelmap_data*/
    NULL,
    0,                                       /* auxiliary data size            */
    0x00,                                    /* used for transparent iamges    */
    38,                                      /* width in pixel                 */
    38                                       /* height in pixel                */
};

GX_CONST GX_PIXELMAP * pixelmap_table[] = 
{
    GX_NULL,
    &PRIMARY_THEME_1_ALPHAMAP_pixelmap
};

int xval;
int yval;
static GX_RESOURCE_ID blend_map = GX_PIXELMAP_ID_ROTATE_FOOT;
VOID sprite_screen_draw(GX_WINDOW *window)
{
    GX_PIXELMAP *map;

    gx_window_draw(window);
    
    gx_context_pixelmap_get(blend_map, &map);
    gx_canvas_pixelmap_blend(xval, yval, map, 200);
}

static GX_CHAR test_comment[256];

#define ALPHAMAP 1
#define PIXELMAP_TABLE_SIZE 2


/* This thread simulates user input.  Its priority is lower
   than the GUIX thread, so that GUIX finishes an operation 
   before this thread is able to issue the next command. */
static VOID control_thread_entry(ULONG input)
{
int                        frame_id = 1;
GX_DISPLAY *display;
INT         offset;
GX_RECTANGLE size;

    gx_widget_style_remove(&((SPRITE_SCREEN_CONTROL_BLOCK *)pSpriteScreen)->sprite_screen_sprite_1, GX_STYLE_SPRITE_AUTO);

    size = pButtonScreen->gx_widget_size;
    size.gx_rectangle_right = 400;
    size.gx_rectangle_bottom = 400;
    
    size = pSpriteScreen->gx_widget_size;
    size.gx_rectangle_left = 300;
    size.gx_rectangle_top = 200;
    gx_widget_resize(pSpriteScreen, &size);
    gx_widget_attach(root, pSpriteScreen);   
    gx_widget_back_move(pSpriteScreen, GX_NULL);
    /* blend pixelmap in different position. */
    display = root->gx_window_root_canvas->gx_canvas_display;

    gx_validation_set_frame_id(frame_id++);
    sprintf(test_comment, "Attach sprite screen to root window.");
    gx_validation_set_frame_comment(test_comment);
    gx_validation_screen_refresh();
    
    gx_widget_draw_set(pSpriteScreen, (void(*)(GX_WIDGET *))sprite_screen_draw);
    
    for(offset = 0; offset <= 640; offset += 300)
    {
        xval = offset;
        yval = offset;
        gx_validation_set_frame_id(frame_id++);
        sprintf(test_comment, "Set draw function to sprite screen and call gx_canvas_pixelmap_blend to blend foot on it. xpos: %d, ypos: %d.", xval, yval);
        gx_validation_set_frame_comment(test_comment);
        gx_system_dirty_mark(root);
        gx_validation_screen_refresh();  
    }
    
    xval = pSpriteScreen->gx_widget_size.gx_rectangle_left;
    yval = pSpriteScreen->gx_widget_size.gx_rectangle_top;
    gx_display_pixelmap_table_set(display, (GX_PIXELMAP **)pixelmap_table, PIXELMAP_TABLE_SIZE);
    blend_map = ALPHAMAP;
    gx_validation_set_frame_id(frame_id++);
    sprintf(test_comment, "Set blend pixelmap alphamap.");
    gx_validation_set_frame_comment(test_comment);
    gx_system_dirty_mark(pSpriteScreen);
    gx_validation_screen_refresh(); 
    
    /* Set gx_display_driver_pixelmap_draw function to null and call gx_canvas_pixelmap_blend to blend.*/
    display->gx_display_driver_pixelmap_draw = GX_NULL;
    display->gx_display_driver_alphamap_draw = GX_NULL;

    xval = pSpriteScreen->gx_widget_size.gx_rectangle_left;
    yval = pSpriteScreen->gx_widget_size.gx_rectangle_top;
    gx_validation_set_frame_id(frame_id++);
    sprintf(test_comment, "Set gx_display_driver_pixelmap_draw function to null and call gx_canvas_pixelmap_blend. xpos: %d, ypos: %d.", xval, yval);
    gx_validation_set_frame_comment(test_comment);
    gx_system_dirty_mark(pSpriteScreen);
    gx_validation_screen_refresh();  

    /* Signal the end of the test case.  Verify the output. */
    gx_validation_end();

    exit(0);
}
